﻿

// GtdRepository
function GtdRepository() {
}

GtdRepository.moveTask = function(pTaskID, pTargetBucketType, callback) {
    $.post('/Task/Move', { taskID: pTaskID, targetBucketType: pTargetBucketType }, callback);
}

GtdRepository.completeTask = function(pTaskID, callback) {
    $.post('/Task/Complete', { taskID: pTaskID }, callback);
}

GtdRepository.saveTask = function (pTask, callback) {
    $.post('/Task/Edit', pTask, callback)
}

GtdRepository.getEditTaskForm = function (pTaskID, callback) {
    $.get('/Task/Edit', { taskID: pTaskID }, callback);
}

GtdRepository.getBucketSizes = function (callback) {
    $.getJSON('/Collection/GetBucketSizes', callback);
}

GtdRepository.loadTaskList = function(ptargetNode, pBucketType, callback) {
    $(ptargetNode).load('/Task/GtdTaskListData', { bucketType: pBucketType }, callback);
}

// EditTaskView
function EditTaskView() {
}

EditTaskView.editTask = function (pTaskID) {
    $('div#task_edit_from').remove();
    var $header = $('<div id="task_edit_from"></div>');
    GtdRepository.getEditTaskForm(pTaskID, function (data) {
        $header.html(data).dialog({
            title: 'Edit Task',
            width: 500,
            draggable: false,
            modal: true,
            buttons: {
                'Save': function () {
                    EditTaskView.saveTask();
                    $(this).dialog('close');
                },
                'Cancel': function () { $(this).dialog('close'); }
            }
        });
    });
}

EditTaskView.saveTask = function() {
    var gtdTask = {
        TaskID: $('#GtdTask_TaskID').val(),
        UserID: $('#GtdTask_UserID').val(),
        TaskType: $('#GtdTask_TaskType').val(),
        ScheduleDate: $('#GtdTask_ScheduleDate').val(),
        Name: $('#GtdTask_Name').val(),
        ProjectID: $('#GtdTask_ProjectID').val(),
        Description: $('#GtdTask_Description').val(),
        PriorityID: $('#GtdTask_PriorityID').val()
    }
    GtdRepository.saveTask(gtdTask, function () {
        IndexCollectionView.loadTasksForSelectedBucket();
    });
}
